package com.dmpp.mes.service.impl;


import com.dmpp.mes.bean.RespEnum;
import com.dmpp.mes.bean.ResponseData;
import com.dmpp.mes.dao.AromaMapper;
import com.dmpp.mes.dao.ManufactorMapper;
import com.dmpp.mes.parameterInfo.Aroma;
import com.dmpp.mes.parameterInfo.Manufactor;
import com.dmpp.mes.service.AromaService;
import com.dmpp.mes.service.ManufactorService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "ManufactorService")

public class ManufactorServiceImpl implements ManufactorService {

    @Autowired
    private ManufactorMapper mapper;

    @Override
    public ResponseData findAll(Manufactor manufactor) {
        ResponseData responseData = new ResponseData();
        PageInfo pageInfo = new PageInfo<>(mapper.findAll(manufactor));
        responseData.setData(pageInfo);
        return responseData;
    }

    @Override
    public ResponseData findByCode(String code) {
        ResponseData responseData = new ResponseData();
        responseData.setData(mapper.findByCode(code));
        return responseData;
    }

    @Override
    public ResponseData insert(Manufactor manufactor) {
        ResponseData responseData = new ResponseData();
        Manufactor manufactor1 = mapper.findByCode(manufactor.getCode());
        if (manufactor1 == null) {
            mapper.insert(manufactor);
        } else {
            responseData.setCodeEnum(RespEnum.ERROR2001);
        }
        return responseData;
    }

    @Override
    public ResponseData updateByCode(Manufactor manufactor) {
        ResponseData responseData = new ResponseData();
        mapper.updateByCode(manufactor);
        return responseData;
    }

    @Override
    public ResponseData deleteByCode(String code) {
        ResponseData responseData = new ResponseData();
        mapper.deleteByCode(code);
        return responseData;
    }

    @Override
    public ResponseData delete(String code) {
        ResponseData responseData = new ResponseData();
        int materialCount = mapper.materialCount(code);
        int semiProductCount = mapper.semiProductCount(code);
        if (materialCount == 0) {
            if (semiProductCount == 0) {
                mapper.delete(code);
            } else {
                responseData.setCodeEnum(RespEnum.ERROR1018);
            }
        } else {
            responseData.setCodeEnum(RespEnum.ERROR1016);
        }

        return responseData;
    }

}
